﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "StructureSlopeLineUiManipulateStrategy.h"
#include "IElement.h"
#include "IModelView.h"
#include "IElementShapeHandle.h"
#include "IGenericElement.h"
#include "IElementBasicInformation.h"
#include "AuxiliaryElementUtils.h"
#include "../StructureSlopeLine.h"
#include "../StructureSlopeLineBehaviors.h"
#include "IGraphicsLine.h"
#include "IGraphicsCurve3d.h"
#include "ElementUiManipulateStrategyRegister.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

std::vector<ElementId> gcmp::StructureSlopeLineUiManipulateStrategy::CreateAuxiliaryElements(gcmp::IModelView* pModelView, const IElement* pElement) const
{
    std::vector<ElementId> res;

    DBG_WARN_AND_RETURN_UNLESS(pModelView, res, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    if (AuxiliaryElementUtils::IsAuxiliaryElementsDisabled(pModelView, pElement))
    {
        return res;
    }

    const IGenericElement* pGenericElement = quick_cast<const IGenericElement>(pElement);
    DBG_WARN_AND_RETURN_UNLESS(pGenericElement, res, L"pGenericElement为空",L"GDMPLab",L"2024-03-30");
    const StructureSlopeLine* pSlopeLine = quick_cast<const StructureSlopeLine>(pGenericElement->GetExternalObject());
    DBG_WARN_AND_RETURN_UNLESS(pSlopeLine, res, L"pSlopeLine为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IGraphicsCurve3d> opCurve = pSlopeLine->GetGraphicsLine();
    DBG_WARN_AND_RETURN_UNLESS(opCurve, res, L"opCurve为空",L"GDMPLab",L"2024-03-30");

    ElementId modelViewId = pModelView->GetElementId();

    Vector3d startPoint = opCurve->GetStartPoint();
    Vector3d endPoint = opCurve->GetEndPoint();

    // 起点
    OwnerPtr<IElementShapeHandleBehavior> pGripPointBehaviorStart = StructureSlopeLineGripPointBehavior::Create(pGenericElement->GetDocument(), modelViewId);
    IElementShapeHandle* pElementShapeHandleStart = IElementShapeHandle::Create(TransferOwnership(pGripPointBehaviorStart), pGenericElement->GetElementId(), startPoint, static_cast<int>(SlopeLineGripPointType::Start));
    DBG_WARN_AND_RETURN_UNLESS(pElementShapeHandleStart != nullptr, res, L"pAuxPtStart为空",L"GDMPLab",L"2024-03-30");
    res.push_back(pElementShapeHandleStart->GetBasicInformation()->GetElementId());

    // 终点
    OwnerPtr<IElementShapeHandleBehavior> pGripPointBehaviorEnd = StructureSlopeLineGripPointBehavior::Create(pGenericElement->GetDocument(), modelViewId);
    IElementShapeHandle* pElementShapeHandleEnd = IElementShapeHandle::Create(TransferOwnership(pGripPointBehaviorEnd), pGenericElement->GetElementId(), endPoint, static_cast<int>(SlopeLineGripPointType::End));
    DBG_WARN_AND_RETURN_UNLESS(pElementShapeHandleEnd != nullptr, res, L"pAuxPtEnd为空",L"GDMPLab",L"2024-03-30");
    res.push_back(pElementShapeHandleEnd->GetBasicInformation()->GetElementId());

    return res;
}

REGISTER_ELEMENT_UIMANIPULATE_STRATEGY(StructureSlopeLine, StructureSlopeLineUiManipulateStrategy);
